/*
 * @Author: 杨三炮
 * @Date: 2021-12-03 17:27:03
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-12-04 12:33:36
 * @FilePath: \daka\src\routers\index.js
 */
//引入vue-router
import VueRouter from 'vue-router'

//引入组件 
import Vue from 'vue';
import { Toast } from 'vant';

Vue.use(Toast);

//创建一个路由器

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/index',
        },
        {
            path: '/index',
            name: 'index',
            component: () => import('../views/Index'),
            meta: { title: '首页' },
            children: [
                {
                    path: '',
                    name: 'home',
                    component: () => import('../views/Home'),
                    meta: { title: '首页' },
                },
                {
                    path: 'mine',
                    name: 'mine',
                    component: () => import('../views/Mine'),
                    meta: { title: '我的信息' },
                }
            ]
        },

        {
            path: '/daka',
            name: 'daka',
            component: () => import('../views/Daka'),
            meta: { title: '健康打卡' }
        },
        {
            path: '/leave',
            name: 'leave',
            component: () => import('../views/Leave'),
            meta: { title: '请假' }
        },
        {
            path: '/dakadetails',
            name: 'dakadetails',
            component: () => import('../views/DakaDetails'),
            meta: { title: '健康打卡' }
        },
        {
            path: '/leavedetails',
            name: 'leavedetails',
            component: () => import('../views/LeaveDetails'),
            meta: { title: '请假' }
        },
        {
            path: '*',
            name: '/404',
            component: () => import('../views/404'),
            meta: { title: '没有该页面' }
        }
    ]
})

//路由守卫 
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.userInfo ? true : false
    if (to.path == '/' || to.path == '/index' || to.path == '/index/mine') {
        next()
    } else {
        if (isLogin) {
            next()
        } else {
            next('/index')
            Toast.fail('请先登录');
        }
    }
})
router.afterEach((to) => {
    document.title = to.meta.title
})
export default router